package com.lxl.settings.controller;

import com.lxl.commons.contants.Contants;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @author 北上寻海
 */
@WebServlet("/exit")
public class ExitServlet extends HttpServlet {

    /**
     * 安全退出的方法，将存入session域中的数据销毁
     * cookie也销毁
     * @param request 请求域
     * @param response 响应域
     * @throws ServletException servlet异常
     * @throws IOException io异常
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        if(session!=null){
            //从Session域中移除Session对象，促发监听,在线人数减少
            session.removeAttribute(Contants.SESSION_USER);
            //手动销毁Session对象,(删除入口验证内容)
            session.invalidate();
        }
        //删除十天免登录的Cookie
        Cookie[] cookies =request.getCookies();
        if(cookies!=null){
            for(Cookie cookie : cookies){
                cookie.setMaxAge(0);
                cookie.setPath(request.getContextPath());
                response.addCookie(cookie);
            }
        }
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }
}

